package com.jfsnpm.plugin.viewoffice;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.jfsnpm.common.AppConfig;

public class ViewOffice {
	public static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="  
            .toCharArray();
	public static String getWOPIClientURL(String fileext) {
		if (fileext == null)
			return null;
		String rooturl = AppConfig.properties.getProperty("wopi_url");
		String xmlurl = rooturl + "hosting/discovery";

		// 获取XML文件
		URL url;
		try {
			url = new URL(xmlurl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.connect();
			InputStream stream = conn.getInputStream();
			/*
			 * BufferedReader reader = new BufferedReader(new InputStreamReader(
			 * stream, "UTF-8")); StringBuffer document = new StringBuffer();
			 * String line = null; while ((line = reader.readLine()) != null) {
			 * document.append(line); }
			 */
			// 解析开始
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docDuilder = factory.newDocumentBuilder();
			Document doc = docDuilder.parse(stream);
			NodeList list = doc.getElementsByTagName("action");
			String urlsrcText = null;
			for (int i = 0; i < list.getLength(); i++) {
				Node node = list.item(i);
				Element actionTag = (Element) node;
				String nameText = actionTag.getAttribute("name");
				String extText = actionTag.getAttribute("ext");
				if (fileext.equals(extText) && "view".equals(nameText)) {
					urlsrcText = actionTag.getAttribute("urlsrc");
					break;
				}
			}
			// 解析结束
			conn.disconnect();
			return urlsrcText;
		} catch (Exception e) {
			return null;
		}
	}

	public static String getFileSHA256(File file) {
		InputStream fis = null;
		String hashString = null;
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-256");
			fis = new FileInputStream(file);
			byte[] buffer = new byte[1024];
			int numRead = 0;
			while ((numRead = fis.read(buffer)) > 0) {
				md.update(buffer, 0, numRead);
			}
			hashString = toHexString(md.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return hashString;
	}
	public static String toHexString(byte[] data) {
		char[] out = new char[((data.length + 2) / 3) * 4];  
        for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {  
            boolean quad = false;  
            boolean trip = false;  
            int val = (0xFF & (int) data[i]);  
            val <<= 8;  
            if ((i + 1) < data.length) {  
                val |= (0xFF & (int) data[i + 1]);  
                trip = true;  
            }  
            val <<= 8;  
            if ((i + 2) < data.length) {  
                val |= (0xFF & (int) data[i + 2]);  
                quad = true;  
            }  
            out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];  
            val >>= 6;  
            out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];  
            val >>= 6;  
            out[index + 1] = alphabet[val & 0x3F];  
            val >>= 6;  
            out[index + 0] = alphabet[val & 0x3F];  
        }  
        return  String.valueOf(out);  
	}
}
